﻿using LeanApp.View;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace LeanApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 通用的命令验证
        /// </summary>
        /// <param name="sender">命令对象</param>
        /// <param name="e">命令参数</param>
        private void Valid_Func(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
        }

        /// <summary>
        /// 功能切换命令验证
        /// </summary>
        /// <param name="sender">命令对象</param>
        /// <param name="e">命令参数</param>
        private void Func_Change(object sender, ExecutedRoutedEventArgs e)
        {
            string ctrlName = e.Parameter as string;
            if (string.IsNullOrWhiteSpace(ctrlName))
            {
                return;
            }
            ChangePage(ctrlName);
        }


        /// <summary>
        /// 自定义控件列表
        /// </summary>
        private static Dictionary<string, UserControl> _ctrl = new Dictionary<string, UserControl>();

        /// <summary>
        /// 更改页面
        /// </summary>
        /// <param name="pageName">页面名称</param>
        private void ChangePage(string pageName)
        {
            workArea.Dispatcher.Invoke(() =>
            {
                UserControl ctrl = GetCtrl(pageName);
                if (workArea.Children.Count > 0)
                {
                    workArea.Children.Clear();
                }
                workArea.Children.Add(ctrl);
            });
        }

        /// <summary>
        /// 获取用户控件
        /// </summary>
        /// <param name="name">组件名</param>
        /// <returns>用户组件</returns>
        /// <exception cref="Exception">组件不存在异常</exception>
        private UserControl GetCtrl(string name)
        {
            if (_ctrl.ContainsKey(name))
            {
                return _ctrl[name];
            }
            else
            {
                switch (name)
                {
                    case "SpcCtrl":
                        SpcCtrl spcCtrl = new SpcCtrl();
                        _ctrl[name] = spcCtrl;
                        return spcCtrl;
                    case "LineBalanceCtrl":
                        LineBalanceCtrl lineBalanceCtrl = new LineBalanceCtrl();
                        _ctrl[name] = lineBalanceCtrl;
                        return lineBalanceCtrl;
                    case "QualityCtrl":
                        QualityCtrl qualityCtrl = new QualityCtrl();
                        _ctrl[name] = qualityCtrl;
                        return qualityCtrl;
                    case "Cumulative":
                        CumulativeCtrl cumulativeCtrl = new CumulativeCtrl();
                        _ctrl[name] = cumulativeCtrl;
                        return cumulativeCtrl;
                    case "Learn":
                        LearnCtrl learnCtrl = new LearnCtrl();
                        _ctrl[name] = learnCtrl;
                        return learnCtrl;
                    default:
                        throw new Exception($@"不存在{name}的用户组件");
                }
            }
        }
    }
}
